package org.loofer.ext.component.info;

import android.content.Context;
import android.os.Build;
import android.os.Build.VERSION;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import org.loofer.ext.Ext;
import org.loofer.ext.utils.NetWorkUtils;
//import io.rong.imlib.statistics.UserData;

public class DeviceDash implements NetworkStateListener {
    private static final String NOT_AVAILABLE = "N/A";
    private static final DeviceDash instance = new DeviceDash();
    private String mDeviceInfo = null;

    public enum CpuArch {
        ARM("armeabi"),
        X86("x86"),
        MIPS("mips"),
        ARM_V7A("armeabi-v7a");
        
        private String type;

        private CpuArch(String type) {
            this.type = type;
        }

        public String toString() {
            return this.type;
        }
    }

    public static DeviceDash getInstance() {
        return instance;
    }

    public DeviceDash() {
        NetworkDash.addListener(this);
    }

    public String getDeviceInfo() {
        if (this.mDeviceInfo == null || this.mDeviceInfo.length() < 1) {
            return updateDeviceInfo();
        }
        return this.mDeviceInfo;
    }

    public String updateDeviceInfo() {
        String apn;
        int i = 1;
        StringBuilder builder = new StringBuilder();
        switch (NetworkDash.getCurrState().getType()) {
            case MOBILE_3G:
                apn = "3g";
                break;
            case MOBILE_2G:
                apn = "2g";
                break;
            case MOBILE_4G:
                apn = "4g";
                break;
            case WIFI:
                apn = NetWorkUtils.APN_NAME_WIFI;
                break;
            case ETHERNET:
                apn = "ethernet";
                break;
            default:
                apn = "wan";
                break;
        }
        String isp = "";
        switch (NetworkDash.getProvider(true)) {
            case NONE:
                isp = "0";
                break;
            case CHINA_MOBILE:
                isp = "1";
                break;
            case CHINA_UNICOM:
                isp = "2";
                break;
            case CHINA_TELECOM:
                isp = "3";
                break;
            case NEVER_HEARD:
                isp = "4";
                break;
        }
        builder.append("imei=").append(getDeviceId()).append('&');
        builder.append("model=").append(Build.MODEL).append('&');
        builder.append("os=").append(VERSION.RELEASE).append('&');
        builder.append("isp=").append(isp).append('&');
        builder.append("apilevel=").append(VERSION.SDK_INT).append('&');
        builder.append("network=").append(apn).append('&');
        StringBuilder append = builder.append("sdcard=");
        if (!StorageDash.hasExternal()) {
            i = 0;
        }
        append.append(i).append('&');
        builder.append("sddouble=").append("0").append('&');
        builder.append("display=").append(getScreenSize()).append('&');
        builder.append("manu=").append(Build.MANUFACTURER).append('&');
        builder.append("wifi=").append(WifiDash.getWifiInfo()).append('&');
        builder.append("cell=").append(NetworkDash.getCellLevel());
        this.mDeviceInfo = builder.toString();
        return this.mDeviceInfo;
    }

    private String getStorageInfo() {
        String str;
        StorageInfo innerInfo = StorageDash.getInnerInfo();
        StorageInfo extInfo = StorageDash.getExternalInfo();
        String str2 = "{IN : %s |EXT: %s}";
        Object[] objArr = new Object[2];
        objArr[0] = innerInfo == null ? "N/A" : innerInfo.toString();
        if (extInfo == null) {
            str = "N/A";
        } else {
            str = extInfo.toString();
        }
        objArr[1] = str;
        return String.format(str2, objArr);
    }

    private static String getDeviceId() {
        try {
            return ((TelephonyManager) Ext.getContext().getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
        } catch (Exception e) {
            return "N/A";
        }
    }

    private static String getScreenSize() {
        WindowManager manager = (WindowManager) Ext.getContext().getSystemService(Context.WINDOW_SERVICE);
        try {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(displayMetrics);
            return "" + displayMetrics.widthPixels + '*' + displayMetrics.heightPixels;
        } catch (Exception e) {
            return "N/A";
        }
    }

    public void onNetworkStateChanged(NetworkState lastState, NetworkState newState) {
        updateDeviceInfo();
    }

    public static CpuArch getCpuBuild() {
        return getCpuArchByAbi(Build.CPU_ABI);
    }

    public static CpuArch getSecondaryCpuBuild() {
        return getCpuArchByAbi(Build.CPU_ABI2);
    }

    public static CpuArch getCpuArchByAbi(String platform) {
        if (platform == null) {
            return CpuArch.ARM;
        }
        if (platform.contains("x86")) {
            return CpuArch.X86;
        }
        if (platform.contains("mips")) {
            return CpuArch.MIPS;
        }
        if (platform.equalsIgnoreCase("armeabi")) {
            return CpuArch.ARM;
        }
        if (platform.equalsIgnoreCase("armeabi-v7a")) {
            return CpuArch.ARM_V7A;
        }
        return CpuArch.ARM;
    }
}
